package com.huawei.esdk.uc.sametime.preferences;

import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.huawei.esdk.uc.sametime.EsdkPluginActivator;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.services.UCSettingsService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;

/**
 * 
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 * 
 * 首选项页-设置麦克风、扬声器设备
 * 
 * @author lWX169831
 * 
 */
public class VoiceSubPreferencePage extends FieldEditorPreferencePage implements
        IWorkbenchPreferencePage
{

    private ComboFieldEditor microphone;

    private ComboFieldEditor speaker;

    private String[][] mircophoneFieldStrings;

    private String[][] speakerFieldStrings;

    public VoiceSubPreferencePage()
    {
        super(GRID);
        setPreferenceStore(EsdkPluginActivator.getDefault()
                .getPreferenceStore());
        setDescription(Messages.VoiceSubPreferencePage_DESCRIPTION);
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common
     * GUI blocks needed to manipulate various types of preferences. Each field
     * editor knows how to save and restore itself.
     */
    public void createFieldEditors()
    {
        mircophoneFieldStrings = UCSettingsService.getMircophoneDevList();
        speakerFieldStrings = UCSettingsService.getSpeakerDevList();

        microphone = new ComboFieldEditor(PreferenceConstants.P_MIRCOPHONE,
                Messages.VoiceSubPreferencePage_MICROPHONE,
                mircophoneFieldStrings, getFieldEditorParent());
        speaker = new ComboFieldEditor(PreferenceConstants.P_SPEAKER,
                Messages.VoiceSubPreferencePage_SPEAKER, speakerFieldStrings,
                getFieldEditorParent());

        addField(microphone);
        addField(speaker);

        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        if (!isUCLogin)
        {
            microphone.setEnabled(false, getFieldEditorParent());
            speaker.setEnabled(false, getFieldEditorParent());
        }

        adjustGridLayout();
    }

    /**
     * 提交“确定”或“应用”按钮，覆盖FieldEditorPreferencePage.performOk方法
     */
    public boolean performOk()
    {
        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        if (!isUCLogin)
        {
            return true;
        }

        IPreferenceStore microphoneStore = microphone.getPreferenceStore();
        IPreferenceStore speakerStore = speaker.getPreferenceStore();
        String microphoneType = microphoneStore
                .getString(PreferenceConstants.P_MIRCOPHONE);
        String speakerType = speakerStore
                .getString(PreferenceConstants.P_SPEAKER);

        /*
         * 这里调用UC SDK的修改麦克风、扬声器设备类型的服务接口
         */
        boolean microphoneDevRes = true;
        if (mircophoneFieldStrings.length > 0)
        {
            microphoneDevRes = UCSettingsService.setMircophoneDev(Integer
                    .parseInt(microphoneType));
        }
        boolean speakerDevRes = true;
        if (speakerFieldStrings.length > 0)
        {
            speakerDevRes = UCSettingsService.setSpeakerDev(Integer
                    .parseInt(speakerType));
        }
        
        if (!microphoneDevRes && speakerDevRes)
        {
            setErrorMessage(Messages.VoiceSubPreferencePage_ERROR_CHANGE_MICROPHONE_INFO_FAIL);
        }
        else if (microphoneDevRes && !speakerDevRes)
        {
            setErrorMessage(Messages.VoiceSubPreferencePage_ERROR_CHANGE_SPEAKER_INFO_FAIL);
        }
        else if (!microphoneDevRes && !speakerDevRes)
        {
            setErrorMessage(Messages.VoiceSubPreferencePage_ERROR_CHANGE_MICROPHONE_AND_SPEAKER_FAIL);
        }
        else
        {
            super.performOk();
        }

        return (microphoneDevRes && speakerDevRes);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench)
    {
    }

}